﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.


namespace Abc.Utils;
/// <summary>
/// Web上传设置
/// </summary>
public class WebUploadSettingOptions : IConfigurableOptionsListener<WebUploadSettingOptions>
{
    /// <summary>
    /// 文件保存路径
    /// </summary>
    public string WebUploadFloder { get; set; }

    /// <summary>
    /// URL访问的逻辑路径 域名/逻辑路径/文件相对路径
    /// 默认:/u/f
    /// </summary>
    public string WebUploadLogicalPath { get; set; }

    /// <summary>
    /// 上传文件是否保存到数据库
    /// </summary>
    public bool WebUploadIsSaveDb { get; set; } = true;

    /// <summary>
    /// 是否限制上传扩展名
    /// </summary>
    public bool IsLimitUploadExtension { get; set; } = true;
    /// <summary>
    /// 是否压缩检测图片是否是真图片，默认不启用，费性能
    /// </summary>
    public bool IsCompressCheckImage { get; set; } = false;
    /// <summary>
    /// 是否启用常见常用文件检测，默认启用
    /// </summary>
    public bool IsFileTypeCheck { get; set; } = true;
    /// <summary>
    /// 是否启用图片压缩
    /// </summary>
    public bool IsImgCompression { get; set; } = true;
    /// <summary>
    /// 图片压缩率，整数，1~100
    /// </summary>
    public int ImgCompressionRatio { get; set; } = 80;

    /// <summary>
    /// 是否生成图片缩略图
    /// </summary>
    public bool IsImgThumbnail { get; set; } = false;

    /// <summary>
    /// 缩略图宽度，高度按比例计算
    /// </summary>
    public int ImgThumbnailWith { get; set; } = 0;

    /// <summary>
    /// 允许上传的文件扩展名
    /// </summary>
    public List<string> AllowExtensions { get; set; } = new List<string>() { ".csv", ".xls", ".xlsx", ".doc", ".docx", ".pdf", ".jpg", ".png", ".jpeg", ".gif" };
    /// <summary>
    /// 图片扩展
    /// </summary>
    public List<string> ImgExtensions { get; set; } = new List<string>() { ".jpg", ".png", ".jpeg", ".gif", ".bmp", ".jfif" };


    public void OnListener(WebUploadSettingOptions options, IConfiguration configuration)
    {
        Type entityType = GetType();//获得该类的Type

        var dics = options.GetDictionary();
        foreach (var item in dics)
        {
            PropertyInfo propertyInfo = entityType.GetProperty(item.Key);
            propertyInfo.FieldSetValue(item.Key, item.Value as object);
        }

        if (ImgExtensions == null) ImgExtensions = new List<string>();
        if (AllowExtensions == null) AllowExtensions = new List<string>();
    }

    public void PostConfigure(WebUploadSettingOptions options, IConfiguration configuration)
    {

    }
}
